package android.fwirk.facebook;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class FacebookUser {
	String id;
	String aboutMe;
	String activities;
	String name;
	String sex;
	String status;
	String interests;
	String relationshipStatus;
	String picUrl;
	String profileUrl;
	String type;
	int lattitude;
	int longitude;
	boolean online;
	Bitmap pic;
	
	public FacebookUser(JSONObject data) {
		parse(data);
	}
	
	public void parse(JSONObject u) {
		id=u.optString("id","0");
		type=u.optString("type","");
		aboutMe=u.optString("about_me", "");
		activities=u.optString("activities", "");
		interests=u.optString("interests", "");
		name=u.optString("name", "");
		picUrl=u.optString("pic", "");
		if(picUrl.equals("")) {
			picUrl=u.optString("pic_square", "");
		}
		picUrl = picUrl.replace("\\", "");
		profileUrl=u.optString("profile_url", "");
		relationshipStatus=u.optString("relationship_status", "");
		sex=u.optString("sex", "");
		JSONObject s = u.optJSONObject("status");
		if (status != null) {
			status = s.optString("message", "");
		} else {
			status = "";
		}
	}
	
	public static List<FacebookUser> getFacebookUsers(JSONArray users) {
		List<FacebookUser> list=null;
		if(users!=null) {
			if(users.length()>0) {
				list=new LinkedList<FacebookUser>();
			}
			FacebookUser u;
			for(int x=0;x<users.length();x++) {
				try {
					JSONObject ui=users.getJSONObject(x);
					u=new FacebookUser(ui);
					list.add(u);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		}
		return list;
	}
	
	public String getAboutMe() {
		return aboutMe;
	}
	public void setAboutMe(String aboutMe) {
		this.aboutMe = aboutMe;
	}
	public String getActivities() {
		return activities;
	}
	public void setActivities(String activities) {
		this.activities = activities;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	public String getInterests() {
		return interests;
	}
	public void setInterests(String interests) {
		this.interests = interests;
	}
	public String getRelationshipStatus() {
		return relationshipStatus;
	}
	public void setRelationshipStatus(String relationshipStatus) {
		this.relationshipStatus = relationshipStatus;
	}
	public String getPicUrl() {
		return picUrl;
	}
	public void setPicUrl(String picUrl) {
		this.picUrl = picUrl;
	}
	
	public void downloadPic() {
		pic=getImage(picUrl);
	}
	
	public String getProfileUrl() {
		return profileUrl;
	}
	public void setProfileUrl(String profileUrl) {
		this.profileUrl = profileUrl;
	}
	public int getLattitude() {
		return lattitude;
	}
	public void setLattitude(int lattitude) {
		this.lattitude = lattitude;
	}
	public int getLongitude() {
		return longitude;
	}
	public void setLongitude(int longitude) {
		this.longitude = longitude;
	}
	public boolean isOnline() {
		return online;
	}
	public void setOnline(boolean online) {
		this.online = online;
	}
	
	public Bitmap getImage(String address) {
		try {
			URL url = new URL(address);
			Object content = url.getContent();
			InputStream is = (InputStream) content;
			Bitmap b = BitmapFactory.decodeStream(is);
			is.close();
			return b;
		} catch (MalformedURLException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
	public Bitmap getPic() {
		return pic;
	}
	public void setPic(Bitmap pic) {
		this.pic = pic;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	
	public static FacebookUser getUser(List<FacebookUser> list,String id) {
		for(FacebookUser u:list) {
			if(u.getId().equals(id)) {
				return u;
			}
		}
		return null;
	}
}
